/**
 *
 */
package com.wedroid.api.handler.impl;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import com.wedroid.api.cache.accessor.ContextPathAccessor;
import com.wedroid.api.handler.ResourceHandler;

/**
 * @author lute
 */
@Component("resourceHandler")
public class ResourceHandlerImpl implements ResourceHandler {

    @Value("#{config['resource.path']}")
    private String resourcePath;

    private ContextPathAccessor contextPathAccessor;

    /**
     * @param contextPathAccessor the contextPathAccessor to set
     */
    public void setContextPathAccessor(
            ContextPathAccessor contextPathAccessor) {
        this.contextPathAccessor = contextPathAccessor;
    }

    /* (non-Javadoc)
     * @see com.wedroid.api.handler.ResourceHandler#resolveResourceUrl(java.lang.String)
     */
    public String resolveResourceUrl(String url) {
        if (StringUtils.isNotBlank(url) && StringUtils.startsWith(url, resourcePath)) {
            return contextPathAccessor.getDownload() + StringUtils.substring(url, resourcePath.length());
        }
        return url;
    }

}
